// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

export enum tableType{
    staticTable = 0,
    moveTable = 1,
    checkMoveTable = 2,
    switchTable = 3
}

@ccclass
export default class tableMediator extends cc.Component {

    @property({displayName : "移动的距离"})
    moveDistance : cc.Vec2 = cc.v2(0,0);

    @property({type : cc.Enum(tableType),displayName : "板子类型"})
    curType : tableType = tableType.staticTable;

    @property()
    isMoveUp = false;


    tableMoveAction(){
        cc.tween(this.node).by(0.3,{
            position : this.moveDistance
        }).start();
    }

    onLoad(){
        if(this.curType == tableType.moveTable){
            this.moveSpeed = -0.1;
        }
    }


    isMoving = false;
    tabelMoveUp(){
        this.isMoving = !this.isMoving;
        if(this.isMoving){
            this.moveSpeed = 1;
        }else{
            this.moveSpeed = -0.1;
        }
    }

    tableAction(){
        this.isMoveUp = !this.isMoveUp;
        if(this.isMoveUp){
            this.moveSpeed = 3;
        }else{
            this.moveSpeed = -3;
        }
    }


    onBeginContact(contact, self, other) {
        if(other.node.group == "roleTrigger" && this.curType == tableType.checkMoveTable){
            this.moveSpeed = 1;
        }
    }

    onEndContact(contact, self, other) {
        if(other.node.group == "roleTrigger" && this.curType == tableType.checkMoveTable){
            this.moveSpeed = -1;
        }
    }

    moveSpeed = 0;
    protected update(dt: number): void {
        if(this.curType == tableType.moveTable || this.curType == tableType.checkMoveTable || this.curType == tableType.switchTable){
            if(this.node.y + this.moveSpeed <= this.moveDistance.x || this.node.y + this.moveSpeed >= this.moveDistance.y){
                this.moveSpeed = 0;
            }
            this.node.y += this.moveSpeed;
        }
    }
}
